//package com.avicit.zipkinkafkaesserver.controller;
//
//import com.alibaba.druid.sql.dialect.h2.visitor.H2ASTVisitor;
//import com.alibaba.fastjson.JSONObject;
//import com.avicit.zipkinkafkaesserver.dao.es.ElasticsearchTemplateWarpper;
//import com.avicit.zipkinkafkaesserver.dao.es.ZipkinInterfaceCountEsDao;
//import com.avicit.zipkinkafkaesserver.entity.ZipkinInterfaceCount;
//import com.avicit.zipkinkafkaesserver.entity.ZipkinUpdateMsg;
//import com.avicit.zipkinkafkaesserver.enums.ESIndexsEnum;
//import com.avicit.zipkinkafkaesserver.service.JobService;
//import com.avicit.zipkinkafkaesserver.service.SparkService;
//import com.avicit.zipkinkafkaesserver.service.ZipkinInterfaceCountService;
//import com.avicit.zipkinkafkaesserver.utils.JsonUtils;
//import com.avicit.zipkinkafkaesserver.utils.TimeUtils;
//import com.avicit.zipkinkafkaesserver.vo.InterfaceCountVo;
//import org.apache.lucene.util.QueryBuilder;
//import org.apache.spark.sql.sources.In;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.common.document.DocumentField;
//import org.elasticsearch.hadoop.rest.SearchRequestBuilder;
//import org.elasticsearch.index.query.*;
//import org.elasticsearch.search.SearchHit;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
//import org.springframework.data.elasticsearch.core.ResultsExtractor;
//import org.springframework.data.elasticsearch.core.query.IndexQuery;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
//import org.springframework.data.elasticsearch.core.query.SearchQuery;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.util.StringUtils;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
//import scala.Tuple2;
//import scala.tools.nsc.typechecker.AnalyzerPlugins;
//
//import java.io.IOException;
//import java.time.LocalDate;
//import java.util.*;
//import java.util.concurrent.TimeUnit;
//import java.util.stream.Collectors;
//
//@Controller
//public class DemoController {
//
//    @Autowired
//    private SparkService sparkService;
//    @Autowired
//    private ElasticsearchTemplate elasticsearchTemplate;
//    @Autowired
//    private ZipkinInterfaceCountEsDao zipkinInterfaceCountEsDao;
//    @Autowired
//    private ZipkinInterfaceCountService zipkinInterfaceCountService;
//    @RequestMapping("/index")
//    public String index() {
//        return "hello";
//    }
//
//    @Autowired
//    private JobService jobService;
//    @GetMapping("/create")
//    @ResponseBody
//    public String create() throws IOException {
////        jobService.doInterfaceCount();
////        jobService.doInterfaceDuration();
////        jobService.doInterfaceAverageDuration();
//        return "success";
//    }
//
//    /**
//     * 按时间段实时 统计数据
//     * @param model
//     * @param startTime
//     * @param endTime
//     * @return
//     * @throws IOException
//     */
//    @RequestMapping("/interfaceCount")
//    public String interfaceCount(Model model,
//                                 @RequestParam(required = false) String startTime,
//                                 @RequestParam(required = false) String endTime,
//                                 @RequestParam(required = false) String interfaceName) throws IOException {
//        long startTimeSecond = 0;
//        long endTimesecond = System.currentTimeMillis();
//        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
//             startTimeSecond = TimeUtils.dateTimeToMilliSecond(startTime, TimeUtils.DatePattern.ALLTIME);
//             endTimesecond = TimeUtils.dateTimeToMilliSecond(endTime, TimeUtils.DatePattern.ALLTIME);
//        }
//        List<Tuple2<String, Integer>> tuple2List = sparkService.interfaceCount(startTimeSecond,endTimesecond,Integer.MAX_VALUE, interfaceName);
//        List<InterfaceCountVo> countVos = tuple2List.stream()
//                .map(tuple2 -> {
//                    String[] split = tuple2._1.split("@");
//                    return InterfaceCountVo.builder()
////                            .ip(split[0])
//                            .serverName(split[0])
//                            .interfaceName(split[2])
////                            .status(split[4])
//                            .count(tuple2._2)
//                            .build();
//                })
//                .collect(Collectors.toList());
//        model.addAttribute("countVos",countVos);
//        return "interfaceCount";
//    }
//
//    /**
//     * 接口平均耗时
//     * @return
//     * @throws IOException
//     */
//    @RequestMapping("/interfaceDurationCount")
//    public String interfaceDurationCount(Model model,
//                                         @RequestParam(required = false) String startTime,
//                                         @RequestParam(required = false) String endTime,
//                                         @RequestParam(required = false, defaultValue = "") String interfaceName
//                                         ) throws IOException {
//        long startTimeSecond = 0;
//        long endTimesecond = System.currentTimeMillis();
//        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
//            startTimeSecond = TimeUtils.dateTimeToMilliSecond(startTime, TimeUtils.DatePattern.ALLTIME);
//            endTimesecond = TimeUtils.dateTimeToMilliSecond(endTime, TimeUtils.DatePattern.ALLTIME);
//        }
//
//        List<Tuple2<String, Double>> tuple2s = (List<Tuple2<String, Double>>)sparkService.interfaceDurationCountNoIp(startTimeSecond,endTimesecond,Integer.MAX_VALUE,interfaceName).get("countResult");
//        List<InterfaceCountVo> countVos = tuple2s.stream().map(tuple2 -> {
//            String[] split = tuple2._1.split("@");
//            double value = tuple2._2;
//            return InterfaceCountVo.builder()
////                    .ip(split[0])
//                    .serverName(split[0])
//                    .interfaceName(split[2])
//                    .duration((int) value)
//                    .build();
//        }).collect(Collectors.toList());
//        model.addAttribute("countVos",countVos);
//        return "interfaceDurationCount";
//    }
//
//    /**
//     * 接口耗时列表
//     * @param min
//     * @param max
//     * @return
//     */
//    @GetMapping("/interfaceDurationCountList")
//    public String interfaceDurationCountList(Model model,
//                                             @RequestParam(required = false,defaultValue = "0") double min,
//                                             @RequestParam(required = false,defaultValue = "0") double max,
//                                             @RequestParam(required = false,defaultValue = "0") int topN,
//                                             @RequestParam(required = false) String startTime,
//                                             @RequestParam(required = false) String endTime,
//                                             @RequestParam(required = false, defaultValue = "") String interfaceName
//                                             ) throws IOException {
//        long startTimeSecond = 0;
//        long endTimesecond = System.currentTimeMillis();
//        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {
//            startTimeSecond = TimeUtils.dateTimeToMilliSecond(startTime, TimeUtils.DatePattern.ALLTIME);
//            endTimesecond = TimeUtils.dateTimeToMilliSecond(endTime, TimeUtils.DatePattern.ALLTIME);
//        }
//        List<Tuple2<String, Double>> tuple2s = sparkService.interfaceDurationCountList(min, max,topN,startTimeSecond, endTimesecond,interfaceName);
//        List<InterfaceCountVo> countVos = tuple2s.stream().map(tuple2 -> {
//            String[] split = tuple2._1.split("@");
//            double value = tuple2._2;
//            return InterfaceCountVo.builder()
////                    .ip(split[0])
//                    .serverName(split[1])
//                    .interfaceName(split[3])
//                    .dateTime(TimeUtils.timestamToDatetime(Long.valueOf(split[4])))
//                    .duration((int) value)
//                    .build();
//        }).collect(Collectors.toList());
//        model.addAttribute("countVos",countVos);
//        return "interfaceDurationCountList";
//    }
//}
